{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d3f54021",
   "metadata": {},
   "source": [
    "# 上周回顾\n",
    "\n",
    "## 1. 人脸识别概念和基础\n",
    "## 2. 人脸识别应用\n",
    "## 3. 人脸识别实践-face++\n",
    "\n",
    "> 1. API文档阅读规范\n",
    "> 2. 人脸检测（人脸分析）实践\n",
    "> 3. 输入/输出\n",
    "> 4. 错误码寻找原因"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "75cd0320",
   "metadata": {},
   "source": [
    "# 本周内容\n",
    "\n",
    "> 1. 人脸对比\n",
    "> 2. 人脸搜索"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bf553b35",
   "metadata": {},
   "source": [
    "# 1. 人脸对比"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d1a7f87a",
   "metadata": {},
   "source": [
    "## 原理和概念\n",
    "## 人脸对比实践"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4ba1269c",
   "metadata": {},
   "source": [
    "* 将两个人脸进行比对，来判断是否为同一个人，返回比对结果置信度和不同误识率下的阈值。支持传入图片或 face_token 进行比对。使用图片时会自动选取图片中检测到人脸尺寸最大的一个人脸。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "925cf982",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "bf5f9059",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'request_id': '1679279782,6f5cabce-9fd4-424b-b6eb-5213b7783b17', 'time_used': 340, 'confidence': 82.138, 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}, 'faces1': [{'face_token': 'a347feec501e67d1a87506e6479d7205', 'face_rectangle': {'top': 131, 'left': 163, 'width': 106, 'height': 106}}], 'faces2': [{'face_token': 'b33ecedb1e9143b5d2888c22bfb28a92', 'face_rectangle': {'top': 143, 'left': 115, 'width': 163, 'height': 163}}], 'image_id1': '40HA22MiijXxwCnrvb/XKw==', 'image_id2': '0qnIsGaxEEgzkH079PBUZg=='}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "Face++ 人脸检测\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "# 二进制方式打开图片文件\n",
    "# 1.图片文件准备\n",
    "f = open('wjk.jpg', 'rb')\n",
    "image_file1 = base64.b64encode(f.read())\n",
    "\n",
    "f = open('王俊凯.jpg', 'rb')\n",
    "image_file2 = base64.b64encode(f.read())\n",
    "\n",
    "# 2.酬载准备\n",
    "payload = {\n",
    "    'api_key':'S3pPsuiHUq8aTVfx5PaN0i9E9A6qZYG2',\n",
    "    'api_secret':'Ikh4sAg-OyGE53GUI-PNZ1fxwEyL1BeK',\n",
    "    'image_base64_1':image_file1,\n",
    "    'image_base64_2':image_file2,\n",
    "}\n",
    "\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())\n",
    "else:\n",
    "    print(response.status_code)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "58347a45",
   "metadata": {},
   "source": [
    "## 人脸数据库介绍"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "189ad9a0",
   "metadata": {},
   "source": [
    "## 人脸数据存储规则"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "07eb348d",
   "metadata": {},
   "source": [
    "## 人脸库管理\n",
    "\n",
    "> 1. 创建创建人脸库\n",
    "> 2. 获取人脸库信息\n",
    "> 3.添加人脸\n",
    "> 4.获取人脸库信息"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e4729eae",
   "metadata": {},
   "source": [
    "# 2. 人脸搜索"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "223cfd30",
   "metadata": {},
   "source": [
    "## 2.1 人脸库管理"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "70819a5c",
   "metadata": {},
   "source": [
    "## 2.1.1 创建人脸数据库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "b0034771",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "aaaaf5c1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200\n",
      "{'faceset_token': '8acd04198e4a76e746f78afbaf4fa33f', 'time_used': 173, 'face_count': 0, 'face_added': 0, 'request_id': '1679316494,65b15726-94a0-48fa-a8d2-303c88b62fa9', 'outer_id': '', 'failure_detail': []}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "\n",
    "'''\n",
    "人脸数据库创建\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "# 请求参数\n",
    "params = {\n",
    "    'api_key':'S3pPsuiHUq8aTVfx5PaN0i9E9A6qZYG2',\n",
    "    'api_secret':'Ikh4sAg-OyGE53GUI-PNZ1fxwEyL1BeK',\n",
    "    'display_name':\"广州南方学院1栋宿舍楼\"\n",
    "}\n",
    "\n",
    "response = requests.post(request_url, data=params)\n",
    "print(response.status_code)\n",
    "print (response.json())\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "908c028c",
   "metadata": {},
   "source": [
    "## 2.1.2 检查人脸数据集的情况"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3fe2d54b",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200\n",
      "{'faceset_token': '0e561d5092917085e1cb2118dabe90c4', 'tags': '', 'time_used': 85, 'user_data': '', 'display_name': '广州南方学院1栋宿舍楼', 'face_tokens': ['d5f72407abcc27a57ff91c260250a17c', '751323bcc5b0e3153e526a8c4d89c84c', '95285941bad89ada34b99f78c1101373'], 'face_count': 3, 'request_id': '1679316510,5491fc3e-e2ef-4e97-ab30-5e4cbcbb8c89', 'outer_id': ''}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "\n",
    "'''\n",
    "人脸数据库创建\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\"\n",
    "# 请求参数\n",
    "params = {\n",
    "    'api_key':'S3pPsuiHUq8aTVfx5PaN0i9E9A6qZYG2',\n",
    "    'api_secret':'Ikh4sAg-OyGE53GUI-PNZ1fxwEyL1BeK',\n",
    "    'faceset_token':\"0e561d5092917085e1cb2118dabe90c4\"\n",
    "}\n",
    "\n",
    "response = requests.post(request_url, data=params)\n",
    "print(response.status_code)\n",
    "print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c319de73",
   "metadata": {},
   "source": [
    "### 2.1.3 增加人脸信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "39552919",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'request_id': '1679316513,27c3772c-6601-4c4b-a917-76da2524f8ad', 'time_used': 100, 'faces': [{'face_token': '496574c90f5c2e02202519d2557cf11e', 'face_rectangle': {'top': 172, 'left': 108, 'width': 204, 'height': 204}}], 'image_id': '48iFavztL7dBwuTd3ngFyA==', 'face_num': 1}\n"
     ]
    }
   ],
   "source": [
    "# 检测人脸信息\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "Face++ 人脸检测:刘亦菲，蔡徐坤，高启强\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "# 二进制方式打开图片文件\n",
    "# 1.图片文件准备\n",
    "f = open('1栋/lyf.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "\n",
    "# 2.酬载准备\n",
    "payload = {\n",
    "    'api_key':'S3pPsuiHUq8aTVfx5PaN0i9E9A6qZYG2',\n",
    "    'api_secret':'Ikh4sAg-OyGE53GUI-PNZ1fxwEyL1BeK',\n",
    "    'image_base64':img,\n",
    "}\n",
    "\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())\n",
    "else:\n",
    "    print(response.status_code)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "9be7dc7a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'request_id': '1679316532,a5b8f288-fa0f-4a8a-bd42-c60c7ee8f44c', 'time_used': 35, 'faces': [{'face_token': 'ff84223edbae41cfac68bfb706d536cf', 'face_rectangle': {'top': 176, 'left': 102, 'width': 202, 'height': 202}}], 'image_id': 'nj8fHfQd55J7EL/H3CIdBQ==', 'face_num': 1}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "Face++ 人脸检测:刘亦菲，蔡徐坤，高启强\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "# 二进制方式打开图片文件\n",
    "# 1.图片文件准备\n",
    "f = open('1栋/cxk.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "\n",
    "# 2.酬载准备\n",
    "payload = {\n",
    "    'api_key':'S3pPsuiHUq8aTVfx5PaN0i9E9A6qZYG2',\n",
    "    'api_secret':'Ikh4sAg-OyGE53GUI-PNZ1fxwEyL1BeK',\n",
    "    'image_base64':img,\n",
    "}\n",
    "\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())\n",
    "else:\n",
    "    print(response.status_code)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "45b1fc23",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'request_id': '1679316533,977cd439-b398-428e-8bd2-19488564fe02', 'time_used': 30, 'faces': [{'face_token': 'eca9e2b718b245d0d767e475389def9c', 'face_rectangle': {'top': 57, 'left': 190, 'width': 146, 'height': 146}}], 'image_id': 'g1hmUBIwfzPsCzLCxflz1g==', 'face_num': 1}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "Face++ 人脸检测:刘亦菲，蔡徐坤，高启强\n",
    "'''\n",
    "\n",
    "request_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "# 二进制方式打开图片文件\n",
    "# 1.图片文件准备\n",
    "f = open('1栋/gqq.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "\n",
    "# 2.酬载准备\n",
    "payload = {\n",
    "    'api_key':'S3pPsuiHUq8aTVfx5PaN0i9E9A6qZYG2',\n",
    "    'api_secret':'Ikh4sAg-OyGE53GUI-PNZ1fxwEyL1BeK',\n",
    "    'image_base64':img,\n",
    "}\n",
    "\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())\n",
    "else:\n",
    "    print(response.status_code)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "82ca21e2",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 'd5f72407abcc27a57ff91c260250a17c','751323bcc5b0e3153e526a8c4d89c84c','95285941bad89ada34b99f78c1101373'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "e440b26d",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "200\n",
      "{'faceset_token': '0e561d5092917085e1cb2118dabe90c4', 'time_used': 528, 'face_count': 3, 'face_added': 1, 'request_id': '1679284543,63225355-5bb7-458b-aff9-d6d26aa74861', 'outer_id': '', 'failure_detail': []}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests\n",
    "'''\n",
    "增加人脸\n",
    "'''\n",
    "request_url = \" https://api-cn.faceplusplus.com/facepp/v3/faceset/addface\"\n",
    "# 请求参数\n",
    "params = {\n",
    "    'api_key':'S3pPsuiHUq8aTVfx5PaN0i9E9A6qZYG2',\n",
    "    'api_secret':'Ikh4sAg-OyGE53GUI-PNZ1fxwEyL1BeK',\n",
    "    'faceset_token':\"0e561d5092917085e1cb2118dabe90c4\",\n",
    "    'face_tokens':'95285941bad89ada34b99f78c1101373'\n",
    "\n",
    "}\n",
    "\n",
    "response = requests.post(request_url, data=params)\n",
    "print(response.status_code)\n",
    "print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2af17fc8",
   "metadata": {},
   "source": [
    "### 2.1.4人脸搜索（课后作业）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4b6f1a1b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
